package de.thwildau.tm.moses.avro.serializer;

import com.google.inject.Inject;
import de.thwildau.tm.moses.avro.services.IdlGrammarAccess;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.IGrammarAccess;
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.nodemodel.INode;
import org.eclipse.xtext.serializer.analysis.GrammarAlias.AbstractElementAlias;
import org.eclipse.xtext.serializer.analysis.GrammarAlias.TokenAlias;
import org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynNavigable;
import org.eclipse.xtext.serializer.analysis.ISyntacticSequencerPDAProvider.ISynTransition;
import org.eclipse.xtext.serializer.sequencer.AbstractSyntacticSequencer;

@SuppressWarnings("all")
public class IdlSyntacticSequencer extends AbstractSyntacticSequencer {

	protected IdlGrammarAccess grammarAccess;
	protected AbstractElementAlias match_RPCMessage_OnewayKeyword_5_q;
	
	@Inject
	protected void init(IGrammarAccess access) {
		grammarAccess = (IdlGrammarAccess) access;
		match_RPCMessage_OnewayKeyword_5_q = new TokenAlias(false, true, grammarAccess.getRPCMessageAccess().getOnewayKeyword_5());
	}
	
	@Override
	protected String getUnassignedRuleCallToken(EObject semanticObject, RuleCall ruleCall, INode node) {
		if(ruleCall.getRule() == grammarAccess.getEndLineRule())
			return getEndLineToken(semanticObject, ruleCall, node);
		else if(ruleCall.getRule() == grammarAccess.getListSeparatorRule())
			return getListSeparatorToken(semanticObject, ruleCall, node);
		else if(ruleCall.getRule() == grammarAccess.getReservedIdentifiersRule())
			return getReservedIdentifiersToken(semanticObject, ruleCall, node);
		return "";
	}
	
	/**
	 * EndLine:
	 * 	';';
	 */
	protected String getEndLineToken(EObject semanticObject, RuleCall ruleCall, INode node) {
		if (node != null)
			return getTokenText(node);
		return ";";
	}
	
	/**
	 * ListSeparator:
	 * 	',';
	 */
	protected String getListSeparatorToken(EObject semanticObject, RuleCall ruleCall, INode node) {
		if (node != null)
			return getTokenText(node);
		return ",";
	}
	
	/**
	 * ReservedIdentifiers:
	 * 	'error' | 'record' | 'enum' | 'fixed' | 'import' | 'throws';
	 */
	protected String getReservedIdentifiersToken(EObject semanticObject, RuleCall ruleCall, INode node) {
		if (node != null)
			return getTokenText(node);
		return "error";
	}
	
	@Override
	protected void emitUnassignedTokens(EObject semanticObject, ISynTransition transition, INode fromNode, INode toNode) {
		if (transition.getAmbiguousSyntaxes().isEmpty()) return;
		List<INode> transitionNodes = collectNodes(fromNode, toNode);
		for (AbstractElementAlias syntax : transition.getAmbiguousSyntaxes()) {
			List<INode> syntaxNodes = getNodesFor(transitionNodes, syntax);
			if(match_RPCMessage_OnewayKeyword_5_q.equals(syntax))
				emit_RPCMessage_OnewayKeyword_5_q(semanticObject, getLastNavigableState(), syntaxNodes);
			else acceptNodes(getLastNavigableState(), syntaxNodes);
		}
	}

	/**
	 * Syntax:
	 *     'oneway'?
	 */
	protected void emit_RPCMessage_OnewayKeyword_5_q(EObject semanticObject, ISynNavigable transition, List<INode> nodes) {
		acceptNodes(transition, nodes);
	}
	
}
